#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import base64
import json

import pytesseract

from common.readelement import Element
from page_object.webpage import WebPage
from tools.logger import Log
from io import BytesIO
from PIL import Image
from tools.ShowapiRequest import ShowapiRequest

login = Element('jzsz/app_enterprise/login')


class AppEnterpriseLoginPage(WebPage):
    """登录类"""
    def choose_enterpriseLogin(self):
        """选择企业登录"""
        self.is_click(login['我的'])
        self.is_click(login['立即登录'])
        self.find_elements(login['企业'])[1].click()

    def username(self, name):
        """用户名"""
        self.find_elements(login['用户名'])[0].click()
        self.find_elements(login['用户名'])[0].send_keys(name)

    def password(self, pwd):
        """密码"""
        self.find_elements(login['用户名'])[1].click()
        self.find_elements(login['用户名'])[1].send_keys(pwd)

    def verification_code(self,screenshot_pic,code_pic):
        """验证码"""
        # self.get_verification_code(screenshot_pic,code_pic)
        # code = self.get_verification_code2(code_pic)
        self.find_elements(login['用户名'])[2].click()
        self.find_elements(login['用户名'])[2].send_keys('1234')

    def submit(self):
        """登录"""
        self.is_click(login['登录'])

    def quit_login(self):
        """退出登录"""
        # num = self.elements_num(login['test'])
        # print('AppPersonalLoginPage', num)
        # self.find_elements(login['test'])[40].click()

        # self.is_click(login['我的'])
        self.is_click(login['设置'])
        self.is_click(login['退出登录'])
        self.is_click(login['确认'])

    def login_success(self):
        """验证登录"""
        self.is_click(login['我的'])
        return self.is_exists(login['求职意向'])



    def get_verification_code2(self, code_pic):
        """
        使用第三方api识别验证码
        """
        # r = ShowapiRequest("http://route.showapi.com/184-1", "619169", "a924d4e982ae404b8a068b4d1c7784f2")
        r = ShowapiRequest("http://route.showapi.com/184-1", "619169", "0f4dc82f59864e4599b724b7c6bb1064")
        r.addFilePara("image", code_pic)
        r.addBodyPara("typeId", "14")
        r.addBodyPara("convert_to_jpg", "0")
        res = r.post()
        print('AppEnterpriseLoginPage',res.text)  # 返回信息

        res_dict = json.loads(res.text)
        img_str = res_dict['showapi_res_body']['Result']
        print('AppEnterpriseLoginPage', img_str)
        print(img_str)  # 打印图片中的字符
        return img_str



    def get_verification_code(self,screenshot_pic,code_pic):
        """
        使用pytesseract库识别验证码
        识别率极低
        """
        self.driver.save_screenshot(screenshot_pic)
        ce = self.find_elements(login['验证吗'])[3]

        left = ce.location['x']
        top = ce.location['y']
        right = ce.size['width'] + left
        height = ce.size['height'] + top

        im = Image.open(screenshot_pic)
        img  = im.crop((left,top,right,height))
        img.save(code_pic)

        # code_img = Image.open(code_pic)
        # str = pytesseract.image_to_string(code_img)
        # print('AppEnterpriseLoginPage', str)




